<%@ CodePage = 65001 %>


<!--#include file="includes/database.asp" -->
<!--#include file="includes/global_varaiable.asp" -->
<!--#include file="staff_info.asp" -->


<%
Response.expires = 0
Response.expiresabsolute = Now() - 1
Response.addHeader "pragma", "no-cache"
Response.addHeader "cache-control", "private"
Response.addHeader "cache-control", "no-cache"
Response.addHeader "cache-control", "no-store"
Response.CacheControl = "no-cache"

nStartRec = 0
nStopRec = 0
nTotalRecs = 0
nRecCount = 0
nRecActual = 0
sDbWhereMaster = ""
sDbWhereDetail = ""
sSrchAdvanced = ""
psearch = ""
psearchtype = ""
sSrchBasic = ""
sSrchWhere = ""
sDbWhere = ""
sOrderBy = ""
sSqlMaster = ""
nDisplayRecs = 20
nRecRange = 10

Const ewSessionMessage = "Message"

' Set up records per page dynamically
SetUpDisplayRecs()

' Handle Reset Command
ResetCmd()

' Build Filter condition
sDbWhere = ""
If (ewCurSec And ewAllowList) <> ewAllowList Then
	sDbWhere = sDbWhere & "(0=1) AND "
End If
If sDbWhereDetail <> "" Then
	sDbWhere = sDbWhere & "(" & sDbWhereDetail & ") AND "
End If
If sSrchWhere <> "" Then
	sDbWhere = sDbWhere & "(" & sSrchWhere & ") AND "
End If
If Len(sDbWhere) > 5 Then
	sDbWhere = Mid(sDbWhere, 1, Len(sDbWhere)-5) ' Trim rightmost AND
End If

' Set Up Sorting Order
sOrderBy = ""
SetUpSortOrder()

' Set up SQL
sSql = ewBuildSql(ewSqlSelect, ewSqlWhere, ewSqlGroupBy, ewSqlHaving, ewSqlOrderBy, sDbWhere, sOrderBy)

'Response.Write sSql ' Uncomment to show SQL for debugging
%>
<!--#include file="header.asp"-->

<script type="text/javascript">
<!--
function EW_selectKey(elem) {
	var f = elem.form;	
	if (!f.key_d) return;
	if (f.key_d[0]) {
		for (var i=0; i<f.key_d.length; i++)
			f.key_d[i].checked = elem.checked;	
	} else {
		f.key_d.checked = elem.checked;	
	}
	ew_clickall(elem);
}
function EW_selected(elem) {
	var f = elem.form;	
	if (!f.key_d) return false;
	if (f.key_d[0]) {
		for (var i=0; i<f.key_d.length; i++)
			if (f.key_d[i].checked) return true;
	} else {
		return f.key_d.checked;
	}
	return false;
}
//-->
</script>
<%

' Set up Record Set

sSql = "SELECT * FROM [NHANSU] WHERE Deleted <> 1"

Set rs = Server.CreateObject("ADODB.Recordset")
rs.CursorLocation = 3
rs.Open sSql, adoCon, 1, 2
nTotalRecs = rs.RecordCount
If nDisplayRecs <= 0 Then ' Display All Records
	nDisplayRecs = nTotalRecs
End If
nStartRec = 1
SetUpStartRec() ' Set Up Start Record Position
%>
<p><span class="aspmaker">TABLE: NHANSU
</span></p>

<table class="ewListAdd">
	<tr>
		<td><span class="aspmaker"><a href="staff_add.asp">Add</a></span></td>
	</tr>
</table>
<p>

<%
If Session(ewSessionMessage) <> "" Then
%>
<p><span class="ewmsg"><%= Session(ewSessionMessage) %></span></p>
<%
	Session(ewSessionMessage) = "" ' Clear message
End If
%>
<form action="staff_list.asp" name="ewpagerform" id="ewpagerform">
<table border="0" cellspacing="0" cellpadding="0">
	<tr>
		<td nowrap>
<%
If nTotalRecs > 0 Then
	rsEof = (nTotalRecs < (nStartRec + nDisplayRecs))
	PrevStart = nStartRec - nDisplayRecs
	If PrevStart < 1 Then PrevStart = 1
	NextStart = nStartRec + nDisplayRecs
	If NextStart > nTotalRecs Then NextStart = nStartRec
	LastStart = ((nTotalRecs-1)\nDisplayRecs)*nDisplayRecs+1
	%>
	<table border="0" cellspacing="0" cellpadding="0"><tr><td><span class="aspmaker">Page&nbsp;</span></td>
<!--first page button-->
	<% If CLng(nStartRec)=1 Then %>
	<td><img src="images/firstdisab.gif" alt="First" width="16" height="16" border="0"></td>
	<% Else %>
	<td><a href="staff_list.asp?start=1"><img src="images/first.gif" alt="First" width="16" height="16" border="0"></a></td>
	<% End If %>
<!--previous page button-->
	<% If CLng(PrevStart) = CLng(nStartRec) Then %>
	<td><img src="images/prevdisab.gif" alt="Previous" width="16" height="16" border="0"></td>
	<% Else %>
	<td><a href="staff_list.asp?start=<%=PrevStart%>"><img src="images/prev.gif" alt="Previous" width="16" height="16" border="0"></a></td>
	<% End If %>
<!--current page number-->
	<td><input type="text" name="pageno" value="<%=(nStartRec-1)\nDisplayRecs+1%>" size="4"></td>
<!--next page button-->
	<% If CLng(NextStart) = CLng(nStartRec) Then %>
	<td><img src="images/nextdisab.gif" alt="Next" width="16" height="16" border="0"></td>
	<% Else %>
	<td><a href="staff_list.asp?start=<%=NextStart%>"><img src="images/next.gif" alt="Next" width="16" height="16" border="0"></a></td>
	<% End If %>
<!--last page button-->
	<% If CLng(LastStart) = CLng(nStartRec) Then %>
	<td><img src="images/lastdisab.gif" alt="Last" width="16" height="16" border="0"></td>
	<% Else %>
	<td><a href="staff_list.asp?start=<%=LastStart%>"><img src="images/last.gif" alt="Last" width="16" height="16" border="0"></a></td>
	<% End If %>
	<td><span class="aspmaker">&nbsp;of <%=(nTotalRecs-1)\nDisplayRecs+1%></span></td>
	</tr></table>
	<% If CLng(nStartRec) > CLng(nTotalRecs) Then nStartRec = nTotalRecs
	nStopRec = nStartRec + nDisplayRecs - 1
	nRecCount = nTotalRecs - 1
	If rsEOF Then nRecCount = nTotalRecs
	If nStopRec > nRecCount Then nStopRec = nRecCount %>
	<span class="aspmaker">Records <%= nStartRec %> to <%= nStopRec %> of <%= nTotalRecs %></span>
	
<% End If %>
		</td>
<% If nTotalRecs > 0 Then %>
		<td nowrap>&nbsp;&nbsp;&nbsp;&nbsp;</td>
		<td align="right" valign="top" nowrap><span class="aspmaker">Records Per Page&nbsp;
<select name="<%= ewTblRecPerPage %>" onChange="this.form.submit();" class="aspmaker">
<option value="10"<% If nDisplayRecs = 10 Then response.write " selected" %>>10</option>
<option value="20"<% If nDisplayRecs = 20 Then response.write " selected" %>>20</option>
<option value="50"<% If nDisplayRecs = 50 Then response.write " selected" %>>50</option>
<option value="100"<% If nDisplayRecs = 100 Then response.write " selected" %>>100</option>
<option value="ALL"<% If Session(ewSessionTblRecPerPage) = -1 Then response.write " selected" %>>All Records</option>
</select>
		</span></td>
<% End If %>
	</tr>
</table>
</form>
<% If nTotalRecs > 0 Then %>
<form method="post">
<table id="ewlistmain" class="ewTable">
	<!-- Table header -->
	<tr class="ewTableHeader">
		<td valign="top"><span>Staff ID</span></td>
		<td valign="top"><span>Username</span></td>
		<td valign="top"><span>Full Name</span></td>
		<td valign="top"><span>Email</span></td>
		<td valign="top"><span>Address</span></td>		
		<td valign="top"><span>Active</span></td>
		
<% If CurrentUserLevel() = 1 Then %>

<td valign="top"><span>Level</span></td>

<% End If %>

		
<% If (CurrentUserLevel() <> 1) or (CurrentUserLevel() <> 2) Then %>

<td>&nbsp;</td>

<% End If %>

<% If (CurrentUserLevel() <> 1) or (CurrentUserLevel() <> 2) Then %>

<td>&nbsp;</td>

<% End If %>

<% If (CurrentUserLevel() <> 1) or (CurrentUserLevel() <> 2) Then %>

<td><input type="checkbox" class="aspmaker" onClick="EW_selectKey(this);"></td>

<% End If %>
</tr>
<%

' Avoid starting record > total records
If CLng(nStartRec) > CLng(nTotalRecs) Then
	nStartRec = nTotalRecs
End If

' Set the last record to display
nStopRec = nStartRec + nDisplayRecs - 1

' Move to first record directly for performance reason
nRecCount = nStartRec - 1
If Not rs.Eof Then
	rs.MoveFirst
	rs.Move nStartRec - 1
End If
nRecActual = 0
Do While (Not rs.Eof) And (nRecCount < nStopRec)
	nRecCount = nRecCount + 1
	If CLng(nRecCount) >= CLng(nStartRec) Then
		nRecActual = nRecActual + 1

	' Set row color
	sItemRowClass = " class=""ewTableRow"""
	
	' Display alternate color for rows
	If nRecCount Mod 2 <> 1 Then
		sItemRowClass = " class=""ewTableAltRow"""
	End If
	x_StaffID = rs("StaffID")
	x_AccUsr = rs("Usr")
	x_AccPwd = rs("Pwd")
	x_Fullname = rs("Fullname")
	x_Email = rs("Email")
	x_Phone = rs("Phone")
	x_Address = rs("Address")
	x_LevelID = rs("Level")
	x_Active = rs("Active")
%>
	<!-- Table body -->
	<tr<%=sItemRowClass%>>
		<!-- OperatorID -->
		<td><span>
<% Response.Write x_StaffID %>
</span></td>
		<!-- AccUsr -->
		<td><span>
<% Response.Write x_AccUsr %>
</span></td>
		<!-- Fullname -->
		<td><span>
<% Response.Write x_Fullname %>
</span></td>
		<!-- Email -->
		<td><span>
<% Response.Write x_Email %>
</span></td>
		<!-- Address -->
		<td><span>
<% Response.Write x_Address %>
</span></td>
		<!-- Active -->
		<td><span>
<%
If x_Active = True Then
	sTmp = "Yes"
Else
	sTmp = "No"
End If
ox_Active = x_Active ' Backup Original Value
x_Active = sTmp
%>
<% Response.Write x_Active %>
<% x_Active = ox_Active ' Restore Original Value
%>
</span></td>

<!-- Level -->
		<td><span>
<% Response.Write x_LevelID %>
</span></td>

<% If (ewCurSec And ewAllowView) = ewAllowView Then %>
<td><span class="aspmaker"><a href="<% If Not IsNull(x_OperatorID) Then Response.Write "staff_view.asp?StaffID=" & Server.URLEncode(x_StaffID) Else Response.Write "javascript:alert('Invalid Record! Key is null');" End If %>">View</a></span></td>
<% End If %>
<% If (ewCurSec And ewAllowEdit) = ewAllowEdit Then %>
<td><span class="aspmaker"><a href="<% If Not IsNull(x_OperatorID) Then Response.Write "staff_edit.asp?StaffID=" & Server.URLEncode(x_StaffID) Else Response.Write "javascript:alert('Invalid Record! Key is null');" End If %>">Edit</a></span></td>
<% End If %>
<% If (ewCurSec And ewAllowDelete) = ewAllowDelete Then %>
<td><span class="aspmaker"><input type="checkbox" name="key_d" value="<%= x_OperatorID %>" class="aspmaker" onclick='ew_clickmultidelete(this);'>Delete</span></td>
<% End If %>
	</tr>
<%
	End If
	rs.MoveNext
Loop
%>
</table>
<% If nRecActual > 0 Then %>
<% If (ewCurSec And ewAllowDelete) = ewAllowDelete Then %>
<p>
<input type="button" name="btndelete" value="DELETE SELECTED" onClick="if (!EW_selected(this)) alert('No records selected'); else {this.form.action='staff_delete.asp';this.form.encoding='application/x-www-form-urlencoded';this.form.submit();}">
<p>
<% End If %>
<% End If %>
</form>
<% End If %>
<%

' Close recordset and adoConection
rs.Close
Set rs = Nothing
adoCon.Close
Set adoCon = Nothing
%>
<!--#include file="footer.asp"-->
<%

'-------------------------------------------------------------------------------
' Function SetUpDisplayRecs
' - Set up Number of Records displayed per page based on Form element RecPerPage
' - Variables setup: nDisplayRecs

Sub SetUpDisplayRecs()
	Dim sWrk
	sWrk = Request.QueryString(ewTblRecPerPage)
	If sWrk <> "" Then
		If IsNumeric(sWrk) Then
			nDisplayRecs = CInt(sWrk)
		Else
			If LCase(sWrk) = "all" Then ' Display All Records
				nDisplayRecs = -1
			Else
				nDisplayRecs = 20 ' Non-numeric, Load Default
			End If
		End If
		Session(ewSessionTblRecPerPage) = nDisplayRecs ' Save to Session

		' Reset Start Position (Reset Command)
		nStartRec = 1
		Session(ewSessionTblStartRec) = nStartRec
	Else
		If Session(ewSessionTblRecPerPage) <> "" Then
			nDisplayRecs = Session(ewSessionTblRecPerPage) ' Restore from Session
		Else
			nDisplayRecs = 20 ' Load Default
		End If
	End If
End Sub

'-------------------------------------------------------------------------------
' Function SetUpSortOrder
' - Set up Sort parameters based on Sort Links clicked
' - Variables setup: sOrderBy, Session(TblOrderBy), Session(Tbl_Field_Sort)

Sub SetUpSortOrder()
	Dim sOrder, sSortField, sLastSort, sThisSort
	Dim bCtrl

	' Check for an Order parameter
	If Request.QueryString("order").Count > 0 Then
		sOrder = Request.QueryString("order")

		' Field [OperatorID]
		If sOrder = "OperatorID" Then
			sSortField = "[OperatorID]"
			sLastSort = Session(ewSessionTblSort & "_x_OperatorID")
			If sLastSort = "ASC" Then sThisSort = "DESC" Else sThisSort = "ASC"
			Session(ewSessionTblSort & "_x_OperatorID") = sThisSort
		Else
			If Session(ewSessionTblSort & "_x_OperatorID") <> "" Then Session(ewSessionTblSort & "_x_OperatorID") = ""
		End If

		' Field [AccUsr]
		If sOrder = "AccUsr" Then
			sSortField = "[AccUsr]"
			sLastSort = Session(ewSessionTblSort & "_x_AccUsr")
			If sLastSort = "ASC" Then sThisSort = "DESC" Else sThisSort = "ASC"
			Session(ewSessionTblSort & "_x_AccUsr") = sThisSort
		Else
			If Session(ewSessionTblSort & "_x_AccUsr") <> "" Then Session(ewSessionTblSort & "_x_AccUsr") = ""
		End If

		' Field [Fullname]
		If sOrder = "Fullname" Then
			sSortField = "[Fullname]"
			sLastSort = Session(ewSessionTblSort & "_x_Fullname")
			If sLastSort = "ASC" Then sThisSort = "DESC" Else sThisSort = "ASC"
			Session(ewSessionTblSort & "_x_Fullname") = sThisSort
		Else
			If Session(ewSessionTblSort & "_x_Fullname") <> "" Then Session(ewSessionTblSort & "_x_Fullname") = ""
		End If

		' Field [Email]
		If sOrder = "Email" Then
			sSortField = "[Email]"
			sLastSort = Session(ewSessionTblSort & "_x_Email")
			If sLastSort = "ASC" Then sThisSort = "DESC" Else sThisSort = "ASC"
			Session(ewSessionTblSort & "_x_Email") = sThisSort
		Else
			If Session(ewSessionTblSort & "_x_Email") <> "" Then Session(ewSessionTblSort & "_x_Email") = ""
		End If

		' Field [Active]
		If sOrder = "Active" Then
			sSortField = "[Active]"
			sLastSort = Session(ewSessionTblSort & "_x_Active")
			If sLastSort = "ASC" Then sThisSort = "DESC" Else sThisSort = "ASC"
			Session(ewSessionTblSort & "_x_Active") = sThisSort
		Else
			If Session(ewSessionTblSort & "_x_Active") <> "" Then Session(ewSessionTblSort & "_x_Active") = ""
		End If
		Session(ewSessionTblOrderBy) = sSortField & " " & sThisSort
		Session(ewSessionTblStartRec) = 1
	End If
	sOrderBy = Session(ewSessionTblOrderBy)
	If sOrderBy = "" Then
		sOrderBy = ewSqlOrderBy
		Session(ewSessionTblOrderBy) = sOrderBy
	End If
End Sub

'-------------------------------------------------------------------------------
' Function SetUpStartRec
' - Set up Starting Record parameters based on Pager Navigation
' - Variables setup: nStartRec

Sub SetUpStartRec()
	Dim nPageNo

	' Check for a START parameter
	If Request.QueryString(ewTblStartRec).Count > 0 Then
		nStartRec = Request.QueryString(ewTblStartRec)
		Session(ewSessionTblStartRec) = nStartRec
	ElseIf Request.QueryString("pageno").Count > 0 Then
		nPageNo = Request.QueryString("pageno")
		If IsNumeric(nPageNo) Then
			nStartRec = (nPageNo-1)*nDisplayRecs+1
			If nStartRec <= 0 Then
				nStartRec = 1
			ElseIf nStartRec >= ((nTotalRecs-1)\nDisplayRecs)*nDisplayRecs+1 Then
				nStartRec = ((nTotalRecs-1)\nDisplayRecs)*nDisplayRecs+1
			End If
			Session(ewSessionTblStartRec) = nStartRec
		Else
			nStartRec = Session(ewSessionTblStartRec)
			If Not IsNumeric(nStartRec) Or nStartRec = "" Then
				nStartRec = 1 ' Reset start record counter
				Session(ewSessionTblStartRec) = nStartRec
			End If
		End If
	Else
		nStartRec = Session(ewSessionTblStartRec)
		If Not IsNumeric(nStartRec) Or nStartRec = "" Then
			nStartRec = 1 'Reset start record counter
			Session(ewSessionTblStartRec) = nStartRec
		End If
	End If
End Sub

'-------------------------------------------------------------------------------
' Function ResetCmd
' - Clear list page parameters
' - RESET: reset search parameters
' - RESETALL: reset search & master/detail parameters
' - RESETSORT: reset sort parameters

Sub ResetCmd()
	Dim sCmd

	' Get Reset Cmd
	If Request.QueryString("cmd").Count > 0 Then
		sCmd = Request.QueryString("cmd")

		' Reset Search Criteria
		If LCase(sCmd) = "reset" Then
			Call ResetSearch()

		' Reset Search Criteria & Session Keys
		ElseIf LCase(sCmd) = "resetall" Then
			Call ResetSearch()

		' Reset Sort Criteria
		ElseIf LCase(sCmd) = "resetsort" Then
			sOrderBy = ""
			Session(ewSessionTblOrderBy) = sOrderBy
			If Session(ewSessionTblSort & "_x_OperatorID") <> "" Then Session(ewSessionTblSort & "_x_OperatorID") = ""
			If Session(ewSessionTblSort & "_x_AccUsr") <> "" Then Session(ewSessionTblSort & "_x_AccUsr") = ""
			If Session(ewSessionTblSort & "_x_Fullname") <> "" Then Session(ewSessionTblSort & "_x_Fullname") = ""
			If Session(ewSessionTblSort & "_x_Email") <> "" Then Session(ewSessionTblSort & "_x_Email") = ""
			If Session(ewSessionTblSort & "_x_Active") <> "" Then Session(ewSessionTblSort & "_x_Active") = ""
		End If

		' Reset Start Position (Reset Command)
		nStartRec = 1
		Session(ewSessionTblStartRec) = nStartRec
	End If
End Sub
%>